package org.ekstrabilet.stadium;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.ekstrabilet.game.beans.Game;
import org.ekstrabilet.stadium.beans.Sector;
import org.ekstrabilet.stadium.beans.Stadium;
import org.ekstrabilet.stadium.beans.Tribune;
import org.ekstrabilet.stadium.constants.Panes;
import org.ekstrabilet.stadium.constants.StadiumConstants;
import org.ekstrabilet.stadium.constants.StadiumState;
import org.ekstrabilet.ticket.CartPane;
import org.ekstrabilet.ticket.beans.Ticket;
/**
 * 
 * @author Maciej Koch
 *
 * Panel contains all stadium preview and selected sector preview.
 */
public class StadiumViewPane extends JPanel implements ActionListener, ChangeListener{
	
	Stadium stadium;
	JPanel all;
	JPanel buttons;
	int state;
	JPanel cards;
	JButton backButton;
	JSlider scale;
	TribunesView tribunesPane;
	SectorsView sectorPane;
	
	public StadiumViewPane(boolean sale, CartPane cart) {	
		tribunesPane = new TribunesView(this);
		sectorPane = new SectorsView(sale, cart);
		init();
	}
	/**
	 * Sets to tribunes state
	 */
	public void restart() {
		setState(StadiumState.TRIBUNES);
	}
	/**
	 * @return sectorPane
	 */
	public SectorsView getSectorView() {
		return sectorPane;
	}
	/**
	 * Sets current state
	 * @param state
	 */
	public void setState(int state) {
		this.state = state;
		
		if(state == StadiumState.TRIBUNES)
			backButton.setVisible(false);
		else
			backButton.setVisible(true);
		switchPane();
	}
	private void init() {
		
		all = new JPanel();
		all.setLayout(new GridLayout(2, 1));
				
		cards = new JPanel();
		cards.setPreferredSize(new Dimension(StadiumConstants.viewWidth, StadiumConstants.viewHeight));

		cards.setLayout(new CardLayout());
		
		cards.add(tribunesPane, Panes.TRIBUNES);
		cards.add(sectorPane, Panes.SECTORS);
		
		all.add(cards);			
		buttons = new JPanel();
		backButton  = new JButton("back");
		backButton.addActionListener(this);
		buttons.add(backButton);
		scale = new JSlider(StadiumConstants.minViewScale,StadiumConstants.maxViewScale,StadiumConstants.sectorViewScale);
		scale.addChangeListener(this);
		buttons.add(scale);
		all.add(buttons);
		
		add(all);
		setState(StadiumState.TRIBUNES);
		switchPane();
	}
	private void switchPane() {
		
		CardLayout cl = (CardLayout) cards.getLayout();
		switch(state) {
		case StadiumState.TRIBUNES:
			cl.show(cards, Panes.TRIBUNES);
			break;
		case StadiumState.SECTORS:
			cl.show(cards, Panes.SECTORS);
			break;
		}
		if(state != StadiumState.SECTORS)
			scale.setVisible(false);
		else
			scale.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		setState(StadiumState.TRIBUNES);
		switchPane();
	}
	public void setGame(Game game) {
		sectorPane.setGame(game);
	}
	public void setStadium(Stadium stadium) {
		this.stadium = stadium;
		tribunesPane.setStadium(stadium);
		sectorPane.setStadium(stadium);
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		sectorPane.setScale(scale.getValue());	
	}

	
	
}
